package com.cskaoyan.market.util.wx;

import com.cskaoyan.market.db.domain.MarketUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * @Author: wutanzi
 * @Date: 2023/5/23 下午4:20
 * @Version: v1.0.0
 * @Description: 获取用户信息的工具类dd
 **/
public class SessionUtil {
    private SessionUtil() {
    }

    public static Session getSession(SecurityManager securityManager) {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        return session;
    }

    public static MarketUser getUser(SecurityManager securityManager) {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        return user;
    }

    public static Integer getUserId(SecurityManager securityManager) {
        MarketUser user = getUser(securityManager);
        if (user == null) {
            return 0;
        }
        return user.getId();
    }
}
